#encoding = utf-8

#主程序

#引入相关的模板
import pygame
import sys
import datetime
import traceback
from random import *
from pygame.locals import *

import player
import monster
import bullet
import panel

from BasicsModules import BasicsMonster

import function

#全局变量
#screen = pygame.Surface.set_mode()
pygame.init()

level = []  #存放关卡信息的列表
winSize = width, height = 800, 600  #游戏窗体大小
win = pygame.display.set_mode(winSize)  #设置游戏窗体
pygame.display.set_caption("闪电猪")   #游戏名称
bgImage = pygame.image.load('images/background.png').convert()  #背景图片
timeDelay = 6
running = True

me = player.Player(winSize, win) #创建玩家
#player.Player.lstPlayer.append(me)  #玩家类中添加玩家对象

clock = pygame.time.Clock() #创建时间轴

#读取关卡，生成怪物
#读取关卡
subLevel = {"levels": 1, "monsters": 3}

#随机生成怪物
#mm1 = BasicsMonster.BasMonster(win, winSize, me, "images/monster/", "little-ghost.png", strName="幽灵2")
#mm2 = BasicsMonster.BasMonster(win, winSize, me, "images/monster/", "little-ghost.png", strName="幽灵2")
mm3 = BasicsMonster.BasMonster(win, winSize, me, "images/monster/", "little-ghost.png", strName="幽灵2")
mm4 = monster.LongRangeMonster(win, winSize, me, "images/monster/", "little-ghost.png", strName="南瓜头", views=100, attackDistance=100)
mm5 = monster.LongRangeMonster(win, winSize, me, "images/monster/", "boss-ghost.png", strName="幽灵王", views=200, attackDistance=200)
bossAttribute = {'baseAttack': 100, 'baseDefense': 20, 'baseSpeed': 1, 'blood': 2000, 'attackInterval': 180, 'bloodHeight': 12, 'width': 80, 'height': 80}

mm5.setAttribute(bossAttribute)

#将怪物存放到类列表中
#BasicsMonster.BasMonster.addMonster(mm1)
#BasicsMonster.BasMonster.addMonster(mm2)
BasicsMonster.BasMonster.addMonster(mm3)
BasicsMonster.BasMonster.addMonster(mm4)
BasicsMonster.BasMonster.addMonster(mm5)

#创建面板
panels = panel.GamePanel(win, me)

#定义主程序函数
def main():
    global timeDelay
    while running:
        checkDirection = False  # 是否按方向键
        # 键盘检测
        key_Press = pygame.key.get_pressed()
        if key_Press[K_w] or key_Press[K_UP]:
            me.moveUp()
            checkDirection = True
        if key_Press[K_s] or key_Press[K_DOWN]:
            me.moveDown()
            checkDirection = True
        if key_Press[K_a] or key_Press[K_LEFT]:
            me.moveLeft()
            checkDirection = True
        if key_Press[K_d] or key_Press[K_RIGHT]:
            me.moveRight()
            checkDirection = True

        #因检测动作太快，开启保护机制，在十分之一秒内不改变攻击方向
        if checkDirection:
            if timeDelay == 6:
                me.attackDirection = me.direction
                timeDelay -= 1
            else:
                if timeDelay <= 0:
                    timeDelay = 6
                else:
                    timeDelay -= 1

        #检测按键
        for event in pygame.event.get():
            if event.type == pygame.QUIT:   #关闭按钮
                print("exit")
                pygame.quit()
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE: #空格键
                    me.attack()

        #画背景
        win.blit(bgImage, (0, 0))

        #画怪物
        intMonsterClear = 0
        lstMonsterClear = []
        for m in BasicsMonster.BasMonster.lstMonster:
            if m.blood <= 0 and m.clear:
                lstMonsterClear.append(intMonsterClear)
            else:
                m.draw()
            intMonsterClear += 1

        #清除死掉的怪物
        for m in lstMonsterClear:
            del BasicsMonster.BasMonster.lstMonster[m]

        #画子弹
        intBulletClear = 0
        lstBulletClear = []
        for b in bullet.Bullet.lstBullets:
            if b.clear == True:
                lstBulletClear.append(intBulletClear)
            else:
                b.draw()
            intBulletClear += 1
        #清除废弃的子弹
        for b in lstBulletClear:
            del bullet.Bullet.lstBullets[b]


        # 画玩家
        #win.blit(me.image, me.rect)
        me.draw()

        #画面板
        panels.draw(me.blood)

    #方向信息清空
        #print(me.direction)
        me.setDirectionNull()
        #print(me.direction)

        pygame.display.flip()
        clock.tick(60)  #每秒运行60次
        #pygame.display.update() #加载更新


#运行时代码
if __name__ == "__main__":
    try:
        main()
    except SystemExit:
        pass
    except:
        traceback.print_exc()
        pygame.quit()
        input()
